package com.youhome.mobile.controller;

import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.mobile.utils.JwtTokenUtil;
import com.youhome.service.model.BClient;
import com.youhome.service.model.BCounselor;
import com.youhome.service.model.CounselorRecommendHouse;
import com.youhome.service.service.CounselorRecommendHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chen365 on 2018/09/17.
 */
@Api(tags = "推荐房源")
@RestController
@RequestMapping(value = "/clients/")
public class CounselorRecommendHouseController {

    @Autowired
    private CounselorRecommendHouseService counselorRecommendHouseService;

    @ApiOperation(value = "列表", notes = "列表", response = CounselorRecommendHouse.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户Id", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "int", name = "pageNumber", value = "当前页码", paramType = "query"),
            @ApiImplicitParam(dataType = "int", name = "pageSize", value = "每页条数", paramType = "query")
    })
    @GetMapping(value = "/{clientId}/houses")
    public Object list(HttpServletRequest request, @PathVariable Long clientId, Integer pageNumber, Integer pageSize) throws CustomException {
        String phone = new JwtTokenUtil().getUsernameFromToken(request);
        return new ResultPage<>(ResultCode.SUCCESS, counselorRecommendHouseService.selectByBean(phone, clientId, pageNumber, pageSize));
    }

    @ApiOperation(value = "推荐", notes = "推荐", response = BClient.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "query"),
            @ApiImplicitParam(dataType = "String", name = "remark", value = "备注", paramType = "query")
    })
    @PostMapping(value = "/{clientId}/houses")
    public Object house(HttpServletRequest request, @PathVariable Long clientId, String houseId, String remark) throws CustomException {
        String usernameFromToken = new JwtTokenUtil().getUsernameFromToken(request);
        counselorRecommendHouseService.recommendHouse(usernameFromToken, clientId, houseId, remark);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除", notes = "删除", response = BCounselor.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Long", name = "clientId", value = "客户ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path")
    })
    @DeleteMapping(value = "/{clientId}/houses/{houseId}")
    public Object delete(HttpServletRequest request, @PathVariable Long clientId, @PathVariable String houseId) throws CustomException {
        String usernameFromToken = new JwtTokenUtil().getUsernameFromToken(request);
        counselorRecommendHouseService.deleteRecommendHouse(usernameFromToken, clientId, houseId);
        return new Result(ResultCode.SUCCESS);
    }
}
